import { BasicDrawer, useDrawerInner, useDrawer } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { DrugAccPeriodMentApi } from "@/api/erp/finance/drugAccPeriodMent";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo, useEffect } from "react";
import { handleTree } from "@/utils/tree";
import { listSimpleDept } from "@/api/system/dept";
import StockInFormDetail from "./StockInFormDetail";
let reload: Function;
let recordId: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const [treeData, setTreeData] = useState([]);
	const en_vi = "drugStorage.bid";
	const formSchema: FormSchema[] = [
		{
			label: t("system.user.deptName"),
			field: "deptId",
			component: "TreeSelect",
			componentProps: {
				treeData,
				fieldNames: {
					value: "id",
					label: "name"
				}
			}
		},
		{
			label: t(`${en_vi}.contractsNo`),
			field: "contractsNo",
			component: "InputSearch",
			componentProps: {
				onSearch: () => openDrawer(true)
			}
		},
		{
			label: t(`${en_vi}.startDate`),
			field: "startDate",
			component: "DatePicker"
		},
		{
			label: t(`${en_vi}.endDate`),
			field: "endDate",
			component: "DatePicker"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t("felix_ui.edit") : t("felix_ui.add");
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields, setFieldValue }] = useForm({
		schemas: formSchema,
		labelWidth: 120,
		baseColProps: {
			span: 24
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		recordId = data.record?.recordId;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.startDate = new Date(formValue.startDate).getTime();
		formValue.endDate = new Date(formValue.endDate).getTime();
		let res;
		if (isUpdated) {
			formValue.recordId = recordId;
			res = await DrugAccPeriodMentApi.updateDrugAccPeriodMent(formValue);
		} else {
			res = await DrugAccPeriodMentApi.createDrugAccPeriodMent(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	useEffect(() => {
		const getTreeData = async () => {
			const data = await listSimpleDept();
			let root: any = { id: 0, name: "部门分类", children: [] };
			root.children = handleTree(data, "id", "parentId");
			let newTreeData = treeData;
			newTreeData.push(root);
			setTreeData(newTreeData);
		};
		getTreeData();
	}, []);
	const [drawerDetail, { openDrawer }] = useDrawer();
	const setOrderItem = (contractsNo: number) => {
		setFieldValue("contractsNo", contractsNo);
	};
	return (
		<>
			<BasicDrawer title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
				<BasicForm register={register} labelWrap={true}></BasicForm>
			</BasicDrawer>
			<StockInFormDetail register={drawerDetail} setOrderItem={setOrderItem}></StockInFormDetail>
		</>
	);
};
export default index;
